package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Friend;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import javax.swing.*;
import java.util.List;

@DubboService
public class FriendsApiImpl implements FriendsApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //添加双向好友关系
    public void save(Long userId, Long friendId) {
        //判断，如果数据库不存在数据，保存数据！！！
        Criteria criteria1 = Criteria.where("userId").is(userId).and("friendId").is(friendId);
        Query query1 = Query.query(criteria1);
        if(!mongoTemplate.exists(query1,Friend.class)) {
            //保存userId到friendId的数据
            Friend friend1 = new Friend();
            friend1.setUserId(userId);
            friend1.setFriendId(friendId);
            friend1.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend1);
        }

        Criteria criteria2 = Criteria.where("userId").is(friendId).and("friendId").is(userId);
        Query query2 = Query.query(criteria2);
        if(!mongoTemplate.exists(query2,Friend.class)) {
            //保存friendId到userId的数据
            Friend friend2 = new Friend();
            friend2.setUserId(friendId);
            friend2.setFriendId(userId);
            friend2.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend2);
        }
    }




    //根据用户id，查询好友列表
    public List<Friend> findByUserId(Long userId, int page, int pagesize) {
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = Query.query(criteria)
                .skip((page - 1) * pagesize)
                .limit(pagesize);
        return mongoTemplate.find(query,Friend.class);
    }



    /*
    * 根据用户id查询好友数量
    * */
    @Override
    public Long findById(Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = Query.query(criteria);
        long count = mongoTemplate.count(query, Friend.class);
        return count;
    }



    /*
    * 取消喜欢
    * */

    @Override
    public void remove(Long userId, Long uid) {
        Criteria criteria = Criteria.where("userId").is(userId).and("friendId").is(uid);
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Friend.class);
        Criteria criteria1 = Criteria.where("userId").is(uid).and("friendId").is(userId);
        Query query1 = Query.query(criteria1);
        mongoTemplate.remove(query1,Friend.class);
    }

    //查询两人是否为好友关系
    @Override
    public Boolean find(Long userId, Long uid) {
        Criteria criteria = Criteria.where("userId").is(userId).and("friendId").is(uid);
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query,Friend.class);
    }













}
